import type { PageParam, PageResult } from '@vben/request';

import { requestClient } from '#/api/request';

export namespace LowcodeTableConfigApi {
  /** 表配置定义 */
  export interface TableConfig {
    id: number;
    tableId: number;
    isParentMenuIdValid: boolean;
    dataSourceConfigId: number;
    scene: number;
    tableName: string;
    tableComment: string;
    remark: string;
    moduleName: string;
    businessName: string;
    className: string;
    classComment: string;
    author: string;
    createTime: Date;
    updateTime: Date;
    templateType: number;
    parentMenuId: number;
  }

  /** 表配置字段定义 */
  export interface TableColumn {
    id: number;
    tableId: number;
    columnName: string;
    dataType: string;
    columnComment: string;
    nullable: number;
    primaryKey: number;
    ordinalPosition: number;
    javaType: string;
    javaField: string;
    dictType: string;
    example: string;
    createOperation: number;
    updateOperation: number;
    listOperation: number;
    listOperationCondition: string;
    listOperationResult: number;
    htmlType: string;
  }

  /** 数据库表定义 */
  export interface DatabaseTable {
    name: string;
    comment: string;
  }

  /** 代码生成详情 */
  export interface CodegenDetail {
    table: TableConfig;
    columns: TableColumn[];
  }

  /** 代码预览 */
  export interface CodegenPreview {
    filePath: string;
    code: string;
  }

  /** 更新代码生成请求 */
  export interface CodegenUpdateReq {
    table: any | TableConfig;
    columns: TableColumn[];
  }

  /** 创建代码生成请求 */
  export interface CodegenCreateListReq {
    dataSourceConfigId?: number;
    tableNames: string[];
  }
}

/** 查询列表代码生成表定义 */
export function getTableConfigList(dataSourceConfigId: number) {
  return requestClient.get<LowcodeTableConfigApi.TableConfig[]>(
    '/infra/codegen/table/list?',
    {
      params: { dataSourceConfigId },
    },
  );
}

/** 查询列表代码生成表定义 */
export function getTableConfigPage(params: PageParam) {
  return requestClient.get<PageResult<LowcodeTableConfigApi.TableConfig>>(
    '/infra/codegen/table/page',
    { params },
  );
}

/** 查询详情代码生成表定义 */
export function getTableConfig(tableId: number) {
  return requestClient.get<LowcodeTableConfigApi.CodegenDetail>(
    '/infra/codegen/detail',
    {
      params: { tableId },
    },
  );
}

/** 修改代码生成表定义 */
export function updateTableConfig(data: LowcodeTableConfigApi.CodegenUpdateReq) {
  return requestClient.put('/infra/codegen/update', data);
}

/** 基于数据库的表结构，同步数据库的表和字段定义 */
export function syncCodegenFromDB(tableId: number) {
  return requestClient.put(`/infra/codegen/sync-from-db?tableId=${tableId}`);
}

/** 预览生成代码 */
export function previewCodegen(tableId: number) {
  return requestClient.get<LowcodeTableConfigApi.CodegenPreview[]>(
    `/infra/codegen/preview?tableId=${tableId}`,
  );
}

/** 下载生成代码 */
export function downloadCodegen(tableId: number) {
  return requestClient.download(`/infra/codegen/download?tableId=${tableId}`);
}

/** 获得表定义 */
export function getSchemaTableList(params: any) {
  // 将 dataSourceConfigId 参数名改为 id，并添加 tableName 搜索参数
  const requestParams: { id: number; tableName?: string } = {
    id: params.dataSourceConfigId || 0,
  };
  
  // 如果有表名称搜索条件，添加到参数中
  if (params.name) {
    requestParams.tableName = params.name;
  }
  
  return requestClient.get<LowcodeTableConfigApi.DatabaseTable[]>(
    '/lowcode/data-source-config/tables',
    { params: requestParams },
  );
}

/** 基于数据库的表结构，创建代码生成器的表定义 */
export function createCodegenList(data: LowcodeTableConfigApi.CodegenCreateListReq) {
  return requestClient.post('/infra/codegen/create-list', data);
}

/** 删除代码生成表定义 */
export function deleteTableConfig(tableId: number) {
  return requestClient.delete('/infra/codegen/delete', {
    params: { tableId },
  });
}

/** 批量删除代码生成表定义 */
export function deleteTableConfigList(tableIds: number[]) {
  return requestClient.delete(
    `/infra/codegen/delete-list?tableIds=${tableIds.join(',')}`,
  );
}
